/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.wizardpager.wizard.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.wizardpager.wizard.model.Page;
import com.example.android.wizardpager.wizard.model.PickContactPage;
import com.example.goodog.R;
import com.example.goodog.customViews.ContactPickerButton;
import com.example.goodog.interfaces.ChosenCallback;

public class PickContactFragment extends Fragment {
	private static final String ARG_KEY = "key";
	private static final int PICK_CONTACT_COMMAND = 1;

    private PageFragmentCallbacks mCallbacks;
    private String mKey;
    private Page mPage;
    private String label,TipName,TipPhone;
    private ContactPickerButton cpb;
    

    public static PickContactFragment create(String key) {
        Bundle args = new Bundle();
        args.putString(ARG_KEY, key);

        PickContactFragment fragment = new PickContactFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public PickContactFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        mKey = args.getString(ARG_KEY);
        mPage = mCallbacks.onGetPage(mKey);

        PickContactPage pickContact = (PickContactPage) mPage;
        label = pickContact.getLabel();
        TipName = pickContact.getTipName();
        TipPhone = pickContact.getTipPhone();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page_empty, container, false);
        ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

        final LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainLL);
        cpb = new ContactPickerButton(getActivity(),PICK_CONTACT_COMMAND,this);
        final EditText Nametv = new EditText(getActivity());
        final EditText Phonetv = new EditText(getActivity());
        Nametv.setHint(TipName);
        Nametv.setLayoutParams(new LayoutParams(300,LayoutParams.WRAP_CONTENT));
        Phonetv.setHint(TipPhone);
        Phonetv.setLayoutParams(new LayoutParams(300,LayoutParams.WRAP_CONTENT));
        
        TextWatcher  Nametw = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
				mPage.getData().putString(Page.TEXT_DATA_KEY, Nametv.getText().toString());
				mPage.notifyDataChanged();
			}

		};
		
		TextWatcher  Phonetw = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				
				mPage.getData().putString(Page.TEXT_DATA_KEY2, Phonetv.getText().toString());
				mPage.notifyDataChanged();
			}

		};
		
		Nametv.addTextChangedListener(Nametw);
		Phonetv.addTextChangedListener(Phonetw);
        
        
        TextView mNameView = ((TextView) rootView.findViewById(R.id.label));
        mNameView.setText(label);

        cpb.callback = new ChosenCallback() {
			
			@Override
			public void NotChosencallbackcall() {

			}
			
			@Override
			public void Chosencallbackcall()
			{
				Nametv.setText(cpb.GetName());
				Phonetv.setText(cpb.GetPhone());
				
				PickContactPage pickContact = (PickContactPage) mPage;
				pickContact.GetCrossPagesBundle().putString("searchString",cpb.GetName());
				 
				mPage.notifyDataChanged();
			}
		};
		
		Nametv.setText(mPage.getData().getString(Page.TEXT_DATA_KEY, ""));
		Phonetv.setText(mPage.getData().getString(Page.TEXT_DATA_KEY2, ""));
		
        ll.addView(cpb);
        ll.addView(Nametv);
        ll.addView(Phonetv);
        
        
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof PageFragmentCallbacks)) {
            throw new ClassCastException("Activity must implement PageFragmentCallbacks");
        }

        mCallbacks = (PageFragmentCallbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }  
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if (requestCode == PICK_CONTACT_COMMAND)
    	{
			cpb.onActivityResult(requestCode, resultCode, data);
		}
    }
    
}
